package org.com.yz.dubbo.web.ctrl;


import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.lang3.StringEscapeUtils;
import org.com.yz.common.annotion.Parameter;
import org.com.yz.common.constant.PayConstant;
import org.com.yz.common.util.ClassRefUtil;
import org.com.yz.common.util.MyLog;
import org.com.yz.common.util.ToolUtil;
import org.com.yz.dal.dao.model.BankCard;
import org.com.yz.dal.dao.model.NosTroAccount;
import org.com.yz.dal.dao.model.Nostro;
import org.com.yz.dubbo.api.service.IBankCardService;
import org.com.yz.dubbo.web.service.CheckWithDrawService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 往账接口控制类
 */
@RestController
public class NostroAccountCtr {


    @Reference(version = "1.0.0", timeout = 30000, retries = 0)
    public IBankCardService bankCardService;

    @Autowired
    private CheckWithDrawService checkWithDrawService;


    private final MyLog _log = MyLog.getLog(NostroAccountCtr.class);


    /**
     * @param data, sign
     * @Description: 往账
     * @author wzc
     * @date 2018/7/16 11:17
     */
    @RequestMapping(value = "/api/pay/nostroAccount.htm")
    @Parameter
    public String nostroAccount(String data, String sign) {
        Nostro mc = new Nostro();
        Nostro nostro = JSONObject.parseObject(StringEscapeUtils.unescapeJava(data), Nostro.class); //转换为对象
        //如果银行卡信息已存在并绑定成功，直接返回
        BankCard bankCard = checkWithDrawService.checkNostro(nostro.getBizContent().getCardNo(), nostro.getBizContent().getMchAccntNo(), nostro.getMchNo(), "1");
        if (ToolUtil.isNotEmpty(bankCard)) {
            NosTroAccount nosTroAccount = new NosTroAccount();
            nosTroAccount.setMchAccntNo(nostro.getBizContent().getMchAccntNo());
            nosTroAccount.setCardNo(nostro.getBizContent().getCardNo());
            nosTroAccount.setAmt(bankCard.getVerifyPhoneCode());
            mc.setBizContent(nosTroAccount);
            return ClassRefUtil.createRetData(nostro, mc, PayConstant.RETURN_DEPOSIT_SUCESS, PayConstant.RETURN_ALIPAY_VALUE_SUCCESS);
        }
        //判断往账金额是否正确
        NosTroAccount nosTroAccount = bankCardService.nostro(nostro);
        if ("2".equals(nosTroAccount.getCode())) {
            mc.setBizContent(null);
            return ClassRefUtil.createRetData(nostro, mc, PayConstant.RETURN_DEPOSIT_NOSTRO_ERROR, PayConstant.RETURN_DEPOSIT_NOSTRO_ERRORS);
        }
        //返回消息
        nosTroAccount = new NosTroAccount();
        nosTroAccount.setMchAccntNo(nostro.getBizContent().getMchAccntNo());
        nosTroAccount.setCardNo(nostro.getBizContent().getCardNo());
        mc.setBizContent(nosTroAccount);
        return ClassRefUtil.createRetData(nostro, mc, PayConstant.RETURN_DEPOSIT_SUCESS, PayConstant.RETURN_ALIPAY_VALUE_SUCCESS);

    }


}


